package com.saasitems.admin.web.controller;

import com.saasitems.admin.model.Matts;
import com.saasitems.admin.model.Original;
import com.saasitems.admin.service.MattsService;
import com.saasitems.admin.service.OriginalService;
import com.saasitems.admin.web.vo.matts.MattsQueryVO;
import com.saasitems.admin.web.vo.matts.MattsVO;
import com.saasitems.admin.web.vo.original.OriginalQueryVO;
import com.saasitems.admin.web.vo.original.OriginalVO;
import com.saasitems.common.model.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by Administrator on 2017/4/17.
 */
@Controller
public class MattsController extends BaseController  {

    @Autowired
    private MattsService mattsService;

    @RequestMapping(value="admin/matts/index/{originalId}",method= RequestMethod.GET)
    public ModelAndView index(@PathVariable("originalId") Integer originalId) {
        PageInfo page = new PageInfo(1, 10);
        MattsQueryVO queryvo = new MattsQueryVO();
        // 必然存在
        queryvo.setOriginalId(originalId);

        List<Matts> list = mattsService.query(queryvo, page);
        Map<String, Object> model = new HashMap<String, Object>();
        model.put("info", list);
        model.put("queryvo", queryvo);
        model.put("page", page);
        return new ModelAndView("admin/matts/index", model);
    }

    @RequestMapping(value="admin/matts/index/{originalId}",method= RequestMethod.POST)
    public ModelAndView index(@PathVariable("originalId") Integer originalId, MattsQueryVO queryVO, PageInfo page) {
        // 必然存在
        queryVO.setOriginalId(originalId);

        List<Matts> list = mattsService.query(queryVO, page);
        Map<String, Object> model = new HashMap<String, Object>();
        model.put("info", list);
        model.put("queryvo", queryVO);
        model.put("page", page);
        return new ModelAndView("admin/matts/index", model);
    }

    @RequestMapping(value="admin/matts/add/{originalId}",method= RequestMethod.GET)
    public ModelAndView add(@PathVariable("originalId") Integer originalId) {
        Map<String, Object> model = new HashMap<String, Object>();
        model.put("originalId", originalId);
        return new ModelAndView("admin/matts/add", model);
    }

    @RequestMapping(value="admin/matts/add/{originalId}",method= RequestMethod.POST)
    @ResponseBody
    public Object add(MattsVO vo, @PathVariable("originalId") Integer originalId, HttpServletRequest request) {
        Map<String, Object> model = new HashMap<String, Object>();

        try {
            vo.setOriginalId(originalId);
            mattsService.save(vo);
            return this.renderSuccess("添加成功", this.convertUrl(request, "admin/matts/index/" + originalId + ".shtml"));
        } catch (Exception e) {
            return this.renderError("添加失败");
        }
    }

    @RequestMapping(value="admin/matts/edit/{originalId}/{id}",method= RequestMethod.GET)
    public ModelAndView edit(@PathVariable("originalId") Integer originalId, @PathVariable("id") Integer id) {
        Map<String, Object> model = new HashMap<String, Object>();
        model.put("originalId", originalId);
        model.put("id", id);

        MattsVO vo = this.mattsService.getInfoById(id);
        model.put("info", vo);
        return new ModelAndView("admin/matts/edit", model);
    }


    @RequestMapping(value="admin/matts/edit/{originalId}/{id}",method= RequestMethod.POST)
    @ResponseBody
    public Object edit(MattsVO vo, @PathVariable("originalId") Integer originalId, @PathVariable("id") Integer id, HttpServletRequest request) {
        Map<String, Object> model = new HashMap<String, Object>();
        model.put("originalId", originalId);
        model.put("id", id);
        vo.setOriginalId(originalId);
        vo.setId(id);

        try {
            this.mattsService.update(vo);
            return this.renderSuccess("更新成功", this.convertUrl(request, "admin/matts/index/" + originalId + ".shtml"));
        } catch (Exception e) {
            return this.renderError("更新失败");
        }
    }
}
